/**
 * Created by lenovo on 2017/12/28.
 */
define(['ngApp', 'ngHttp', 'jquery', 'ngConstant', 'ngAlert','bootstrap'], function (ngApp, ngHttp, JQ, ngConstant, ngAlert) {
    ngApp.directive('onFinishRenderFilters', function ($timeout) {
        return {
            restrict: 'A',
            link: function (scope, element, attr) {
                if (scope.$last === true) {
                    $timeout(function () {
                        scope.$emit('ngRepeatFinished');
                    });
                }
            }
        }
    });

    ngApp.controller("leftTab2", function ($scope, $http, $timeout, $rootScope, $state, $uibModal, commonFn, $cookies,$window) {
        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            $rootScope.leftTabHeight = $('#leftTab2').height();
            $('.tabBox').height($rootScope.leftTabHeight);
        });



        //查询数据的函数封装
        function gettableDataData(){
            if (!$scope.searchClicked) {
                $http({
                url: ngConstant.interfaceHost + 'manager/item/findSearch',
                method: 'GET',
                params:{
                    "sort":"createDate,desc",
                    "page":$scope.page,
                    "size":ngConstant.size
                },
                headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    //页数数据
                    $scope.pageMsg  = res.data.data;
                    //表格数据
                    $scope.tableData = res.data.data.content;
                    //全选
                    $scope.checked = [];
                    $scope.checkedMsg = [];
                    $scope.selectAll = function () {
                        if($scope.select_all) {
                            $scope.checked = [];
                            $scope.checkedMsg = [];
                            angular.forEach($scope.tableData, function (i) {
                                i.checked = true;
                                $scope.checked.push(i.id);
                                $scope.checkedMsg.push({"id":i.id});
                            })
                        }else {
                            angular.forEach($scope.tableData, function (i) {
                                i.checked = false;
                                $scope.checked = [];
                                $scope.checkedMsg = [];
                            })
                        }
                    };
                    $scope.selectOne = function ($event) {
                        angular.forEach($scope.tableData, function (i) {
                            var index = $scope.checked.indexOf(i.id);
                            if(i.checked && index === -1) {
                                $scope.checked.push(i.id);
                                $scope.checkedMsg.push({"id":i.id});
                            } else if (!i.checked && index !== -1){
                                $scope.checked.splice(index, 1);
                                $scope.checkedMsg.splice(index, 1);
                            }
                        });
    
                        if ($scope.tableData.length === $scope.checked.length) {
                            $scope.select_all = true;
                        } else {
                            $scope.select_all = false;
                        }
                    };
                });
            }  else {
                $scope.searchMsg.page = $scope.page;
                $scope.searchMsg.size = ngConstant.size;
                $http({
                    url: ngConstant.interfaceHost + 'admin/item/findAllEnableItemSearch',
                    method: 'GET',
                    params:$scope.searchMsg,
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    //页数数据
                    $scope.pageMsg  = res.data.data;
                    //表格数据
                    $scope.tableData = res.data.data.content;
                    //全选
                    $scope.checked = [];
                    $scope.checkedMsg = [];
                    $scope.selectAll = function () {
                        if($scope.select_all) {
                            $scope.checked = [];
                            $scope.checkedMsg = [];
                            angular.forEach($scope.tableData, function (i) {
                                i.checked = true;
                                $scope.checked.push(i.id);
                                $scope.checkedMsg.push({"id":i.id});
                            })
                        }else {
                            angular.forEach($scope.tableData, function (i) {
                                i.checked = false;
                                $scope.checked = [];
                                $scope.checkedMsg = [];
                            })
                        }
                    };
                    $scope.selectOne = function ($event) {
                        angular.forEach($scope.tableData, function (i) {
                            var index = $scope.checked.indexOf(i.id);
                            if(i.checked && index === -1) {
                                $scope.checked.push(i.id);
                                $scope.checkedMsg.push({"id":i.id});
                            } else if (!i.checked && index !== -1){
                                $scope.checked.splice(index, 1);
                                $scope.checkedMsg.splice(index, 1);
                            }
                        });
    
                        if ($scope.tableData.length === $scope.checked.length) {
                            $scope.select_all = true;
                        } else {
                            $scope.select_all = false;
                        }
                    };
                });




            }
            
           
        }

        function getCarSelectMsg(){
            $http({
                url: ngConstant.interfaceHost + 'admin/carBrand/findAllNormalBrand',
                method: 'GET',
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                $scope.carStyle = res.data.data;
                // $scope.carStyleCode =  $scope.carStyle[0].code;
                $http({
                    url: ngConstant.interfaceHost + 'admin/carModel/findAllNormalModelByBrandCode',
                    method: 'GET',
                    params:{
                        "brandCode":$scope.carStyleCode
                    },
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    $scope.carType = res.data.data;
                    // $scope.carTypeCode =  $scope.carType[0].code;

                    // $http({
                    //     url: ngConstant.interfaceHost + 'admin/carStyle/findAllNormalStyleByCarModel',
                    //     method: 'GET',
                    //     params:{
                    //         "carModel":$scope.carTypeCode,
                    //         "year":$scope.carYear
                    //     },
                    //     headers: {'Authorization':$cookies.get("token")}
                    // }).then(function(res){
                    //     $scope.carModel = res.data.data;
                    //     // $scope.carModelCode =  $scope.carModel[0].code;
                    // });

                });

            });

            var selectAllCar= document.getElementsByName("select1")[0];
            var selectAllCarCode= selectAllCar .options[selectAllCar.selectedIndex].text;
            if (selectAllCarCode == '所有汽车') {
                $('.hideSelect').css({'display':'none'});
            } else {
                $('.hideSelect').css({'display':'block'});
            }

            $scope.carStyleChange = function(){
                if ( $scope.carStyleCode == null) {
                    $('.hideSelect').css({'display':'none'});
                } else {
                    $('.hideSelect').css({'display':'block'});
                }
                $http({
                    url: ngConstant.interfaceHost + 'admin/carModel/findAllNormalModelByBrandCode',
                    method: 'GET',
                    params:{
                        "brandCode":$scope.carStyleCode
                    },
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    $scope.carType = res.data.data;
                    // $scope.carTypeCode =  $scope.carType[0].code;

                    $http({
                        url: ngConstant.interfaceHost + 'admin/carStyle/findAllNormalStyleByCarModel',
                        method: 'GET',
                        params:{
                            "carModel":$scope.carTypeCode,
                            "year":$scope.carYear
                        },
                        headers: {'Authorization':$cookies.get("token")}
                    }).then(function(res){
                        $scope.carModel = res.data.data;
                        // $scope.carModelCode =  $scope.carModel[0].code;


                    });

                });

            };

            $scope.carTypeChange = function(){
                $http({
                    url: ngConstant.interfaceHost + 'admin/carStyle/findAllNormalStyleByCarModel',
                    method: 'GET',
                    params:{
                        "carModel":$scope.carTypeCode,
                        "year":$scope.carYear
                    },
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    $scope.carModel = res.data.data;
                    // $scope.carModelCode =  $scope.carModel[0].code;


                });
            };

            $scope.carTimeChange = function(){
                $http({
                    url: ngConstant.interfaceHost + 'admin/carStyle/findAllNormalStyleByCarModel',
                    method: 'GET',
                    params:{
                        "carModel":$scope.carTypeCode,
                        "year":$scope.carYear
                    },
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    $scope.carModel = res.data.data;
                    // $scope.carModelCode =  $scope.carModel[0].code;


                });
            };
        }
        getCarSelectMsg();


        function getCarSelectMsg2() {
            $http({
                url: ngConstant.interfaceHost + 'admin/itemBrand/findAllNormalBrand',
                method: 'GET',
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                $scope.meterialBrand = res.data.data;
                // $scope.meterialBrandCode =  $scope.meterialBrand[0].code;
            });


            //分组
            $http({
                url: ngConstant.interfaceHost + 'admin/itemGroup/findAllNormalGroup',
                method: 'GET',
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                $scope.meterialGrope = res.data.data;
                // $scope.meterialGropeCode =  $scope.meterialGrope[0].code;
            });

            //分类
            $http({
                url: ngConstant.interfaceHost + 'admin/itemCategory/findAllNormalCategoryByItemBrandCode',
                method: 'GET',
                params:{
                    "itemBrandCode":$scope.meterialBrandCode,
                    "itemGroupCode":$scope.meterialGropeCode
                },
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                $scope.meterialClassify = res.data.data;
                // $scope.meterialClassifyCode =  $scope.meterialClassify[0].code;
            });

            function getCategory(){
                $http({
                    url: ngConstant.interfaceHost + 'admin/itemCategory/findAllNormalCategoryByItemBrandCode',
                    method: 'GET',
                    params:{
                        "itemBrandCode":$scope.meterialBrandCode,
                        "itemGroupCode":$scope.meterialGropeCode
                    },
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    $scope.meterialClassify = res.data.data;
                    // $scope.meterialClassifyCode =  $scope.meterialClassify[0].code;
                });
            }

            $scope.meterialBrandChange = function(){
                getCategory();

            };

            $scope.meterialGroupChange = function(){
                getCategory();
            };
        }
        getCarSelectMsg2();

        //时间
        var list = {};
        list.arr =
            [
                2018,2017, 2016, 2015, 2014, 2013, 2012 ,2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001,2000,1999,1998,1997,'无'
            ];

        $scope.listYear = list.arr;

        //点击增加出弹层 END
        $scope.createNewItemBtn = function(){
            $scope.layerOutShow = true;

            //点击弹层按钮时  需要向后台发送的数据
            $scope.creatNewItemMsg = {};

            //弹层中下拉组件数据获取
            //前三个
            getCarSelectMsg();

            //中间两个 耗材  品牌  分组
            getCarSelectMsg2();


            //点击弹层按钮函数
            $scope.createNewItemSub = function(){

                var select1 = document.getElementsByName("select1")[0];
                var select1bel= select1 .options[select1.selectedIndex].text;
                if (select1bel == '所有汽车') {
                    $scope.creatNewItemMsg.commonFlag = 1;
                } else {
                    $scope.creatNewItemMsg.commonFlag = 0;
                    $scope.creatNewItemMsg.carBrandCode = select1bel;
                }

                var select2 = document.getElementsByName("select2")[0];
                var select2bel= select2 .options[select2.selectedIndex].text;
                $scope.creatNewItemMsg.carModelCode = select2bel;


                var select3 = document.getElementsByName("select3")[0];
                var select3bel= select3 .options[select3.selectedIndex].text;
                $scope.creatNewItemMsg.carStyleCode = select3bel;


                var select4 = document.getElementsByName("select4")[0];
                var select4bel= select4 .options[select4.selectedIndex].text;
                $scope.creatNewItemMsg.itemBrandCode = select4bel;

                var select5 = document.getElementsByName("select5")[0];
                var select5bel= select5 .options[select5.selectedIndex].text;
                $scope.creatNewItemMsg.itemGroupCode = select5bel;

                var select6 = document.getElementsByName("select6")[0];
                var select6bel= select6 .options[select6.selectedIndex].text;
                $scope.creatNewItemMsg.itemCategoryCode = select6bel;

                var yearselect = document.getElementsByName("yearselect")[0];
                var yearble= yearselect .options[yearselect.selectedIndex].text;
                $scope.creatNewItemMsg.year = yearble;

                $scope.creatNewItemMsg.carStyleId = $scope.carModelCode;
                $scope.creatNewItemMsg.itemCategoryId = $scope.meterialClassifyCode;


                if (select1bel !== '所有汽车') {
                    if (!$scope.creatNewItemMsg.carStyleId) {
                        ngAlert.errorMsg('请选择车款');
                        return false;
                    } else if (!$scope.creatNewItemMsg.itemCategoryId) {
                        ngAlert.errorMsg('请选择材料分类');
                        return false;
                    } else if (!$scope.creatNewItemMsg.year) {
                        ngAlert.errorMsg('请选择时间');
                        return false;
                    }
                }


                console.log($scope.creatNewItemMsg);
                $http({
                    url: ngConstant.interfaceHost + 'manager/item/createItem',
                    method: 'POST',
                    data:$scope.creatNewItemMsg,
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    ngAlert.successMsg(res.data.data);
                    $timeout(function(){
                        $scope.layerOutShow = false;
                    },2000);
                    gettableDataData();
                },function(res){
                    ngAlert.errorMsg(res.data.errMsg);
                });

            };


        };


        //搜索
        $scope.searchMsg = {};
        $scope.searchMsg.size = ngConstant.size;
        $scope.searchClicked = false;
        $scope.searcBtnClick = function(){
            $scope.searchClicked = true;
            var select1 = document.getElementsByName("searchSelect1")[0];
            var select1bel= select1 .options[select1.selectedIndex].text;
            if (select1bel == '全部') {
                $scope.searchMsg.carBrandCode = '';
            } else {
                $scope.searchMsg.carBrandCode = select1bel;
            }

            var select2 = document.getElementsByName("searchSelect2")[0];
            var select2bel= select2 .options[select2.selectedIndex].text;
            if (select2bel == '全部') {
                $scope.searchMsg.carModelCode = '';
            } else {
                $scope.searchMsg.carModelCode = select2bel;
            }
            // $scope.searchMsg.carModelCode = select2bel;


            var select3 = document.getElementsByName("searchSelect3")[0];
            var select3bel= select3 .options[select3.selectedIndex].text;

            if (select3bel == '全部') {
                $scope.searchMsg.carStyleCode = '';
            } else {
                $scope.searchMsg.carStyleCode = select3bel;
            }

            // $scope.searchMsg.carStyleCode = select3bel;


            var select4 = document.getElementsByName("searchSelect4")[0];
            var select4bel= select4 .options[select4.selectedIndex].text;
            if (select4bel == '全部') {
                $scope.searchMsg.itemBrandCode = '';
            } else {
                $scope.searchMsg.itemBrandCode = select4bel;
            }
            // $scope.searchMsg.itemBrandCode = select4bel;

            var select5 = document.getElementsByName("searchSelect5")[0];
            var select5bel= select5 .options[select5.selectedIndex].text;
            if (select5bel == '全部') {
                $scope.searchMsg.itemCategoryCode = '';
            } else {
                $scope.searchMsg.itemCategoryCode = select5bel;
            }

            var searchyear = document.getElementsByName("yearselect")[0];
            var yearble= searchyear .options[searchyear.selectedIndex].text;
            if (yearble == '全部') {
                $scope.searchMsg.year = '';
            } else {
                $scope.searchMsg.year = yearble;
            }

            console.log($scope.searchMsg);

            $http({
                url: ngConstant.interfaceHost + 'admin/item/findAllEnableItemSearch',
                method: 'GET',
                params:$scope.searchMsg,
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                // if(res.data.data.content.length == 0) {
                //     ngAlert.successMsg("暂无数据");
                // } else {
                //     $scope.pageMsg = res.data.data;
                //     $scope.tableData = res.data.data.content;
                // }
                $scope.pageMsg = res.data.data;
                $scope.tableData = res.data.data.content;

            });
        };


        //获取table列表
        $scope.page = 0;
        $scope.page1 = $scope.page + 1;
        $http({
            url: ngConstant.interfaceHost + 'manager/item/findSearch',
            method: 'GET',
            params:{
                "sort":"createDate,desc",
                "page":$scope.page,
                "size":ngConstant.size
            },
            headers: {'Authorization':$cookies.get("token")}
        }).then(function(res){
            //页数数据
            $scope.pageMsg  = res.data.data;
            $scope.tableData = res.data.data.content;
            //全选
            //点击下一页上一页时   $scope.select_all初始化置空   在点击事件中写入
            $scope.checked = [];
            $scope.checkedMsg = [];
            $scope.selectAll  = function () {
                if($scope.select_all) {
                    $scope.checked = [];
                    $scope.checkedMsg = [];
                    angular.forEach($scope.tableData, function (i) {
                        i.checked  = true;
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    })
                }else {
                    angular.forEach($scope.tableData, function (i) {
                        i.checked  = false;
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                    })
                }
            };
            $scope.selectOne = function ($event) {
                angular.forEach($scope.tableData, function (i) {
                    var index = $scope.checked.indexOf(i.id);
                    if(i.checked  && index === -1) {
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    } else if (!i.checked  && index !== -1){
                        $scope.checked.splice(index, 1);
                        $scope.checkedMsg.splice(index, 1);
                    }
                });

                if ($scope.tableData.length === $scope.checked.length) {
                    $scope.select_all  = true;
                } else {
                    $scope.select_all  = false;
                }
            };

            //下一页
            $scope.nextPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg .totalPages - 1) {
                    return false;
                } else {
                    $scope.page++;
                    $scope.page1++;
                    gettableDataData();
                }

            };

            //上一页
            $scope.prevPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                } else {
                    $scope.page--;
                    $scope.page1--;
                    gettableDataData();
                }
            };

            //转到第几页
            $scope.myKeyup13  = function(e){
                $scope.select_all = false;
                //IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
                var keycode = window.event?e.keyCode:e.which;
                if(keycode==13){
                    $scope.page = $scope.page1 - 1;
                    gettableDataData();
                }
            };

            //首页
            $scope.firstPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                }
                $scope.page = 0;
                $scope.page1 = 1;
                gettableDataData();
            };

            //尾页
            $scope.lastPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg .totalPages - 1){
                    return false;
                }
                $scope.page = $scope.pageMsg .totalPages - 1;
                $scope.page1 = $scope.pageMsg .totalPages;
                gettableDataData();
            };

            //批量删除品牌
            $scope.deleteTableDataBtn = function(){
                if ($scope.checkedMsg.length == 0) {
                    ngAlert.errorMsg('请选择要删除的数据');
                    return false;
                }
                ngAlert.open('确认删除本条记录？');
                $('.layui-layer-btn').click(function(){
                    $http({
                        url: ngConstant.interfaceHost + 'manager/item/deleteItem',
                        method: 'POST',
                        data:angular.toJson($scope.checkedMsg) ,
                        headers: {'Authorization':$cookies.get("token")}
                    }).then(function(res){
                        ngAlert.successMsg(res.data.data);
                        gettableDataData();
                    });
                });
            };



        });


        $scope.closeLay = function(){
            $scope.layerOutShow = false;
        }


    })
});